package iPersistence.test.dao;

import iPersistence.io.Resources;
import iPersistence.sqlSession.SqlSession;
import iPersistence.sqlSession.SqlSessionFactory;
import iPersistence.sqlSession.SqlSessionFactoryBuilder;
import iPersistence.test.pojo.User;
import org.dom4j.DocumentException;

import java.beans.IntrospectionException;
import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Feuler
 */
public class UserDaoImpl implements UserDao {

    private final SqlSession sqlSession;

    public UserDaoImpl() throws PropertyVetoException, DocumentException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        this.sqlSession = sqlSessionFactory.openSession();
    }

    @Override
    public List<User> findAll() throws IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, InstantiationException, SQLException, NoSuchMethodException, NoSuchFieldException {
        return sqlSession.selectList("user.selectList");
    }

    @Override
    public User findByCondition(User user) throws IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, SQLException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
        return sqlSession.selectOne("user.selectOne", user);

    }
}
